package com.xiaolei.baselib.base;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;

import com.xiaolei.baselib.annotation.AssetOpenInputStream;
import com.xiaolei.baselib.util.AnnotationUtil;

import android.app.Activity;
import android.content.Intent;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

public abstract class BaseV4Fragment extends Fragment
{
    private String TAG = "BaseFragment";
    private String className = this.getClass().getSimpleName();
    private AssetManager mAssetManager;
    private Field fields[] = null;

    @Override
    public void onActivityCreated(Bundle savedInstanceState)
    {
        super.onActivityCreated(savedInstanceState);
        init();
    }

    private void init()
    {
        Log.i(TAG + ":onCreate", className);
        try
        {
            initAnotationViews();
        } catch (IllegalAccessException e)
        {
            e.printStackTrace();
        } catch (java.lang.InstantiationException e)
        {
            e.printStackTrace();
        } catch (IOException e)
        {
            e.printStackTrace();
        }
        initViews();
        setListeners();
        initDatas(getActivity().getIntent());
        loadDatas();
    }

    public BaseV4Activity getBaseActivity()
    {
        return (BaseV4Activity) getActivity();
    }

    private void initAnotationViews() throws IllegalAccessException, java.lang.InstantiationException, IOException
    {
        fields = this.getClass().getDeclaredFields();

        for (int i = 0; i < fields.length; i++)
        {
            Field field = fields[i];
            if (field.isAnnotationPresent(AssetOpenInputStream.class))//是打开资源文件夹的注解
            {
                if (field.getType() == InputStream.class)
                {
                    field.setAccessible(true);//设置私有的  可访问属性 为true
                    AssetOpenInputStream aso = field.getAnnotation(AssetOpenInputStream.class);
                    String name = aso.value();
                    if (mAssetManager == null)
                    {
                        mAssetManager = getActivity().getAssets();
                    }
                    InputStream inputStream = mAssetManager.open(name);
                    field.set(this, inputStream);
                }
            } else
            {
                Object object = AnnotationUtil.getFragmentAnnotationObject(this, field);
                field.setAccessible(true);//设置私有的  可访问属性 为true
                if (object != null)
                    field.set(this, object);
            }
        }
    }

    /**
     * 实例化界面
     */
    public abstract void initViews();

    /**
     * 设置监听事件
     */
    public abstract void setListeners();

    /**
     * 初始化数据
     *
     * @param intent
     */
    public abstract void initDatas(Intent intent);

    /**
     * 加载耗时数据
     */
    public abstract void loadDatas();

    public void startActivity(Class<? extends Activity> activity)
    {
        startActivity(activity, null);
    }

    public void startActivity(Class<? extends Activity> activity, Intent intent)
    {
        Intent intent1 = intent == null ? new Intent() : new Intent(intent);
        intent1.setClass(getActivity(), activity);
        startActivity(intent1);
    }

    public <T extends View> T findViewByID(int id)
    {
        return (T) getView().findViewById(id);
    }

    private Toast toast = null;

    protected void Alert(String msg)
    {
        if (toast == null)
        {
            toast = Toast.makeText(getActivity(), "", Toast.LENGTH_SHORT);
        }
        toast.setText(msg);
        toast.show();
    }

}
